package StudyCode.summary.mycollection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsAnIntContainingElementsSharedBetweenTheTwoArrays {
	public static void main(String[] argv) throws Exception {
		int[] arra = new int[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int[] arrb = new int[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		System.out.println(java.util.Arrays.toString(commonElements(arra,
				arrb)));
	}

	public static int[] commonElements(int[] arra, int[] arrb) {

		int[] c = null;
		int n = countCommonElements(arra, arrb);

		if (n > 0) {
			c = new int[n];

			int k = 0;

			for (int i = 0; i < arra.length; i++) {
				for (int j = 0; j < arrb.length; j++) {
					if (arra[i] == arrb[j]) {
						c[k++] = arra[i];
					}
				}
			}
		}

		return c;
	}

	public static int countCommonElements(int[] arra, int[] arrb) {

		int k = 0;

		for (int i = 0; i < arra.length; i++) {
			for (int j = 0; j < arrb.length; j++) {
				if (arra[i] == arrb[j]) {
					k++;
				}
			}
		}

		return k;
	}
}
